package com.breakfast.fun.ui.user.login;

import com.breakfast.fun.data.entity.user.Result;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 16/9/26.
 */

public class ForgetPresenter extends ForgetContract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    public void getCode(String mobile) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "findCode2");
        map.put("mobile", mobile);

        showLoadingDialog(true);

        rxManager.add(model.getCodeModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("发送失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        hideLoadingDialog();
                        if (result.isSuccess()) {
                            view.sendCodeSuccess();
                        } else {
                            T.show(result.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void resetPwd(String mobile, String code, String pwd) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "resetPass2");
        map.put("mobile", mobile);
        map.put("password", pwd);
        map.put("code", code);

        showLoadingDialog(true);

        rxManager.add(model.getResetPwdModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("重置失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        hideLoadingDialog();
                        if (result.isSuccess()) {
                            view.resetPwdSuccess();
                        } else {
                            T.show(result.getMsg());
                        }
                    }

                }));
    }
}
